/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Base} from 'base.js';
import {Config} from 'config.js';

class Address extends Base {
    constructor() {
        super();
        this._storageKeyName = 'addressInfo';
        this._addressList = 'addressList';
    }
    /**
     * 获取收货地址
     */
    getAddress(callback){
        var that = this;
        var param = {
            url: 'min/address/all',
            sCallback: function(res){
                if(res) {
                    for (let i in res) {
                        res[i].totalDetail = that.setAddressInfo(res[i]); // 封装地址详情
                    }
                    callback && callback(res);
                }
                // 存到本地缓存
                that.execSetStorageNameSync(res);
            }
        };
        this.request(param);
    }
    /*
     * 根据省市县信息组装地址信息
     * provinceName, receiverState 前者为微信选择控件返回结果，后者为查询地址时自己服务器后台返回结果
     */
    setAddressInfo(res){
        var province = res.provinceName || res.receiverState, // 省
            city = res.cityName || res.receiverCity, // 市
            country = res.countyName || res.receiverDistrict, // 区/县
            detail = res.detailInfo || res.receiverAddress; // 详细地址
        var totalDetail = province +' '+ city +' '+ country +' '+ detail;
        // 直辖市
        if(this.isCenterCity(province)){
            totalDetail = province +' '+ country.replace(/\s+/g,"") +' '+ detail;
        }
        return totalDetail;
    }
    /**
     * 是否为直辖市
     */
    isCenterCity(name){
        var centerCitys = '北京市,天津市,上海市,重庆市',
            flag = centerCitys.indexOf(name) >= 0;
        return flag;
    }
    /**
     * 增加保存一键获取地址
     */
    submitAddress(data, type, callback){  // 0一键保存 1手动保存
        if (type != 1) {
            data = this._setUpAddress(data);
        }
        var param={
            url: '/min/address/add',
            type: 'post',
            data: data,
            sCallback: function(res) {
                callback && callback(true, res);
            },
            eCallback(res){
                callback && callback(false, res);
            }
        };
        this.request(param);
    }
    /**
     * 更新地址
     */
    updateAddress(data, callback){
        var param={
            url: '/min/address/update',
            type: 'post',
            data: data,
            sCallback: function(res) {
                callback && callback(true, res);
            },
            eCallback(res){
                callback && callback(false, res);
            }
        };
        this.request(param);
    }
    /**
     * 重新组装请求保存地址数据{}
     */
    _setUpAddress(res){
        var formData = {
            receiverName: res.userName,
            receiverMobile: res.telNumber,
            receiverState: res.provinceName,
            receiverCity: res.cityName,
            receiverDistrict: res.countyName,
            receiverAddress: res.detailInfo,
            receiverZip: res.postalCode,
            df: 0
        }
        return formData
    }
    /**
     * 删除地址
     */
    deleteAddress(id, callback){
        var param={
            url: '/min/address/delete?id='+id,
            sCallback: function(res) {
                callback && callback(true, res);
            },
            eCallback(res){
                callback && callback(false, res);
            }
        };
        this.request(param);
    }
    /**
     * 设置默认
     */
    setDefault(id, callback){
        var param={
            url: '/min/address/updateDf?id='+id,
            sCallback: function(res) {
                callback && callback(true, res);
            },
            eCallback(res){
                callback && callback(false, res);
            }
        };
        this.request(param);
    }
    /**
     * 获取本地缓存 地址
     */
    getAddressFromLocal(){
        return wx.getStorageSync(this._storageKeyName)
    }
    /**
     * 本地缓存 保存/更新
     */
    execSetStorageSync(data){
        wx.setStorageSync(this._storageKeyName, data)
    }
    /**
     * 获取本地缓存 地址
     */
    getAddressListFromLocal(){
        return wx.getStorageSync(this._addressList)
    }
    /**
     * 本地缓存 保存/更新
     */
    execSetStorageNameSync(data){
        wx.setStorageSync(this._addressList, data)
    }
    /**
     * 获取省市区数据
     */
    getProvinceData(callback){
        var that = this;
        var param = {
            url: '/area/getProvince',
            sCallback: function(res){
                callback && callback(res);
            }
        }
        this.request(param);
    }
    getCityData(id,callback){
        var that = this;
        var param = {
            url: '/area/getCity?parentId='+id,
            sCallback: function(res){
                callback && callback(res);
            }
        }
        this.request(param);
    }
    getDistrictData(id,callback){
        var that = this;
        var param = {
            url: '/area/getArea?parentId='+id,
            sCallback: function(res){
                callback && callback(res);
            }
        }
        this.request(param);
    }
}

export {Address};